package labb3;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SpringLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;

/**
 * Creates a panel with the labels needed to show information about a Media
 * object.
 *
 * @author Mikko Puustinen
 * @version 0.4, 2007-okt-17
 */
class CenterPanel extends JScrollPane implements Labels {
    private final JPanel panel = new JPanel(new BorderLayout());
    private final JLabel mediaTitle = new JLabel();
    private final JLabel mediaProductionDate = new JLabel();
    private final JLabel mediaReleaseDate = new JLabel();
    private final JLabel mediaPurchaseDate = new JLabel();
    private final JLabel mediaRating = new JLabel();
    private final JLabel[] mediaLabels = {mediaTitle, mediaProductionDate, mediaReleaseDate, mediaPurchaseDate, mediaRating};

    /**
     * Constructs a JPanel and puts it in a JScrollPane.
     *
     * @see javax.swing.JScrollPane
     */
    public CenterPanel() {
        int labelLength = labels.length;
        panel.setLayout(new SpringLayout());
        int i = 0;
        for (String label : labels) {
            JLabel l = new JLabel(label, JLabel.LEADING);
            l.setFont(new Font(Font.DIALOG, Font.BOLD, 12));
            panel.add(l);
            l.setLabelFor(mediaLabels[i]);
            mediaLabels[i].setFont(new Font(Font.DIALOG, Font.PLAIN, 12));
            panel.add(mediaLabels[i]);
            i++;
        }
        SpringUtilities.makeCompactGrid(panel,
            labelLength, 2, //rows, cols
            6, 6,        //initX, initY
            6, 6);       //xPad, yPad
        setViewportView(panel);
        panel.setBackground(Color.WHITE);
    }

    /**
     * Updates the panel and sets the correct label with the information from
     * the specified Media object.
     *
     * @param media Media to show in the panel.
     */
    public void updatePanel(Media media) {
        mediaTitle.setText(media.getTitle());
        mediaProductionDate.setText(media.getProductionDate());
        mediaReleaseDate.setText(media.getReleaseDate());
        mediaPurchaseDate.setText(media.getPurchasedDate());
        mediaRating.setText(media.getRating());
    }

    /**
     * Clears the panel from Media information.
     */
    public void resetPanel() {
        mediaTitle.setText("");
        mediaProductionDate.setText("");
        mediaReleaseDate.setText("");
        mediaPurchaseDate.setText("");
        mediaRating.setText("");
    }
}
